package fr.upstlse.glre.client.secretaire.actualite;

import java.util.List;

import com.extjs.gxt.ui.client.Events;
import com.extjs.gxt.ui.client.binder.DataListBinder;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelStringProvider;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.fx.Draggable;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.DataList;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;

import fr.upstlse.glre.client.constante.fields.ActualiteConstante;
import fr.upstlse.glre.client.secretaire.actualite.action.ActualiteSelectionAction;
import fr.upstlse.glre.client.secretaire.component.EspaceSecretaireCenter;
import fr.upstlse.glre.client.utilitaires.ControlerUtil;
import fr.upstlse.glre.client.vo.actualite.ActualiteVOGWT;
import fr.upstlse.glre.client.widgets.modelData.ActualiteModelData;

public class ModificationActualitePanel extends Composite {
	private ContentPanel modificationPanel;

	/**
	 * Panneau FORM contenant le profil au format formulaire pour l'édition.
	 */
	private DataList modificationList;

	private ContentPanel actualiteModificationPanel;

	private List<ActualiteVOGWT> actualiteVOGWTs;

	private static ModificationActualitePanel instance;
	
	public ModificationActualitePanel() {
		ModificationActualitePanel.setInstance(this);

		createModificationPanel();
		createActualiteModificationPanel();
		modificationList = new DataList();

		modificationPanel.add(modificationList);
		modificationPanel.add(actualiteModificationPanel);
		initWidget(modificationPanel);
	}

	private void createModificationPanel() {
		modificationPanel = new ContentPanel();
		modificationPanel.setAutoWidth(true);
		modificationPanel.setWidth(500);
		modificationPanel
				.setId(ActualiteConstante.MODIFICATION_PANEL_ID.getValue());
		modificationPanel.setBorders(true);
		modificationPanel.setCollapsible(true);
		modificationPanel.collapse();

		// Draggable panel
		Draggable lDraggable = new Draggable(modificationPanel,
				modificationPanel.getHeader());
		lDraggable.setContainer(EspaceSecretaireCenter.getInstance());

		modificationPanel
				.setHeading(ActualiteConstante.MODIFICATION_PANEL_HEADER_TEXT.getValue());

		modificationPanel.addListener(Events.Expand, new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				createModificationList();
			}
		});
	}

	private void createModificationList() {
		if (actualiteVOGWTs == null) {
			final MessageBox lAttente = MessageBox.alert("Information",
					"Chargement des actualités en cours...", null);
			AsyncCallback<List<ActualiteVOGWT>> lCallback = new AsyncCallback<List<ActualiteVOGWT>>() {

				public void onFailure(Throwable pCaught) {
					lAttente.hide();
					MessageBox.alert("Erreur", "Erreur getAllLibelle "
							+ pCaught.getMessage(), null);
				}

				public void onSuccess(List<ActualiteVOGWT> pResult) {
					lAttente.hide();
					actualiteVOGWTs = pResult;

					final ListStore<ActualiteModelData> lStore = new ListStore<ActualiteModelData>();
					lStore.add(ActualiteModelData.toActualiteModelDatas(actualiteVOGWTs));
					DataListBinder<ActualiteModelData> binder = new DataListBinder<ActualiteModelData>(
							modificationList, lStore);
					binder
							.setDisplayProperty(ActualiteConstante.MODELDATA_TITRE.getValue());
					binder
							.setStringProvider(new ModelStringProvider<ModelData>() {

								public String getStringValue(ModelData model,
										String property) {
									String lTexte = (String) model
											.get(ActualiteConstante.MODELDATA_TITRE.getValue());
									return lTexte;
								}

							});
					binder.init();

					modificationList.addListener(Events.SelectionChange,
							new ActualiteSelectionAction());

					Info.display("Information",
							"Succès du chargement des actualités", "");
				}

			};
			ControlerUtil.getControlerServiceAsyncInstance().getAllActualite(lCallback);
		}
	}

	private void createActualiteModificationPanel() {
		actualiteModificationPanel = new ContentPanel();
		actualiteModificationPanel.setAutoWidth(true);
		actualiteModificationPanel.setWidth(500);
		actualiteModificationPanel
				.setId(ActualiteConstante.MODIFICATION2_PANEL_ID.getValue());
		actualiteModificationPanel.setBorders(true);
		actualiteModificationPanel.setHeaderVisible(false);
	}

	public ContentPanel getModificationPanel() {
		return modificationPanel;
	}

	public void setModificationPanel(ContentPanel pModificationPanel) {
		modificationPanel = pModificationPanel;
	}

	public DataList getModificationList() {
		return modificationList;
	}

	public void setModificationList(DataList pModificationList) {
		modificationList = pModificationList;
	}

	public ContentPanel getLibelleModificationPanel() {
		return actualiteModificationPanel;
	}

	public void setLibelleModificationPanel(ContentPanel pLibelleModificationPanel) {
		actualiteModificationPanel = pLibelleModificationPanel;
	}

	public List<ActualiteVOGWT> getActualiteVOGWTs() {
		return actualiteVOGWTs;
	}

	public void setActualiteVOGWTs(List<ActualiteVOGWT> pActualiteVOGWTs) {
		actualiteVOGWTs = pActualiteVOGWTs;
	}

	public static ModificationActualitePanel getInstance() {
		if (instance == null) {
			instance = new ModificationActualitePanel();
		}

		instance.getModificationPanel().layout();
		return instance;
	}

	public static void setInstance(ModificationActualitePanel pInstance) {
		instance = pInstance;
	}
	

}
